<?php
namespace App\HttpController\Pudu;

use App\Common\Controller\Rest;
use think\Db;

class Shops extends Rest
{
    public $modelName = 'Shop';   // model 类名称

    public function POSTIndex()
    {
        // 通过省总名称 获得省总 id 没有则插入 有就获得
        $this->param['governorId'] = $this->getGovernorId($this->param['governor']);
        $this->param['mayorId'] = $this->getMayorId($this->param['mayor']);
        $res = $this->model()->createData($this->param);
        $this->result($res, ['msg' => self::$model->getError()]);
    }

    private function getGovernorId($governor)
    {
        $governorInfo = Db::table('pudu_governor')->where(['governor' => $governor])->find();
        if($governorInfo){
            return $governorInfo['id']; 
        }else{
            $res = Db::table('pudu_governor')->insert(['governor'=>$governor]);
            return $res;
        }
    }

    private function getMayorId($mayor)
    {
        $mayorInfo = Db::table('pudu_mayor')->where(['mayor' => $mayor])->find();
        if($mayorInfo){
            return $mayorInfo['id']; 
        }else{
            $res = Db::table('pudu_mayor')->insert(['mayor'=>$mayor]);
            return $res;
        }
    }
}
